<?xml version="1.0" encoding="UTF-8"?>
<html>
	<head>
		<link type="text/css" rel="stylesheet" href="./css/template.css" />
		<link type="text/css" rel="stylesheet" href="./css/SyntaxHighlighter.css" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>What is J2ME?</title>
		<script language="javascript" src="./js/shInit.js" />
		<script language="javascript" src="./js/shCore.js" />
		<script language="javascript" src="./js/shBrushCpp.js" />
		<script language="javascript" src="./js/shBrushCSharp.js" />
		<script language="javascript" src="./js/shBrushCss.js" />
		<script language="javascript" src="./js/shBrushDelphi.js" />
		<script language="javascript" src="./js/shBrushJava.js" />
		<script language="javascript" src="./js/shBrushJScript.js" />
		<script language="javascript" src="./js/shBrushPhp.js" />
		<script language="javascript" src="./js/shBrushPython.js" />
		<script language="javascript" src="./js/shBrushRuby.js" />
		<script language="javascript" src="./js/shBrushSql.js" />
		<script language="javascript" src="./js/shBrushVb.js" />
		<script language="javascript" src="./js/shBrushXml.js" />
		<author>Fabrice GRONDIN</author>
	    <keywords>J2ME,java,micro,edition,sun</keywords>
	</head>

	<body>
		<div class="chapter">
			<h2>1. Introduction</h2>
			<p>Everybody mainly knows Java as a server programming environment, concentrated around the technologies which realised Java 2 Enterprise Edition (J2EE), such the Enterprise JavaBeans (EJB), the Servlets, and the Java Server Pages (JSP). However, this language was foremost made as environment for the "client-side" applications. Actually Java was born at first for the household appliance. Currently, with the Java 2 Micro Edition, a.k.a J2ME, Java brings back to its roots.</p>
		</div>

		<div class="chapter">
			<h2>2. What realy is Java 2 Micro Edition?</h2>
			<p>
				<strong>Java 2 Micro Edition</strong> as known as J2ME is a version of the Java 2 platform for the household appliance and for the embed systems.<br />
				Since the terminals haven't got the same resources capacities in comparison with the classical desktop computers (memory, hard drive and computing power), the solution go through the supplying of a lightened environment in order to fit to the several execution constraints.<br />
				<strong>J2ME</strong> is consequently the <strong>Java</strong> framework specialized in mobiles applications. Nowadays, a lot of <u>cell phone</u> and <u>PDA</u> have got an embed J2ME compatible with Java platforms.<br />
                <br />
				A J2ME platform is composed	by:<br />
			</p>
			<ul>
	            <li>a "virtual machine" able to execute Java application. It has to be light in order to consume less resource. Lot of virtual machines already exists like <strong>KVM</strong> for Kilobyte Virtual Machine, or <strong>CVM</strong> for C-Virtual Machine, and so one...</li>
                <li>a "configuration", an API given the access to the system's basics functions.</li>
                <li>a "profile", an API given the access to the specifics functions of this platform.</li>
	        </ul>
	        <br />
	        <img src="images/j2me_html_154678a8.png" alt="" width="100%" />
	    </div>

	    <div class="chapter">
	        <h2>3. The configurations</h2>
	        <p>
				The configuration is the <strong>basement of J2ME</strong>. It is built up by the virtual machine and by the low level libraries. Currently there are two types of configurations: CDC and CLDC. Their use is function of the type of the material where the Java environment will be run. The configurations can be stacked, in such way that what can be run on a CLDC configuration can also be run on a CDC one. Nevertheless, the reverse isn't true.
			</p>
	        <div class="subChapter">
	            <h3>3.1. CDC (Connected Device Configuration)</h3>
	            <p>
					<strong>CDC</strong> is best fit for the terminals relatively powerful like PDAs. Indeed, it needs an optimized java virtual machine called CVM which offers as much features as the classical JVM.<br />
					<br />
					The environment material characteristics proposed by the CDC configuration are:
				</p>
				<ul>
	                <li>At least <strong>512Ko</strong> ROM and <strong>256Ko</strong> RAM, also a <strong>32 bits</strong> processor</li>
                    <li><strong>A mandatory network connection</strong> (wireless or not)</li>
                    <li>Support of the all the Java virtual machine specifications (CVM)</li>
				</ul>
			</div>
			<div class="subChapter">
				<h3>3.2. <strong>CLDC</strong> (Connected Limited Device Configuration)</h3>
				<p>
					The CLDC is however dedicated to the low capacities equipments like cell phones, personal assistants or light wireless peripherals. The lighten virtual machine corresponding is the KVM and doesn&rsquo;t have all the classical JVM functions.<br />
					<br />
					The following list gives the general characteristics:<br />
				</p>
				<ul>
					<li>At least <strong>160Ko to 512Ko</strong> RAM, <strong>16 or 32 bits</strong> processor, <strong>16Mhz</strong> speed (or more)</li>
					<li><strong>Limited alimentation</strong>, loaded by a battery.</li>
					<li>A <strong>network connection not permanent</strong>, wireless.</li>
					<li>A limited or inexisting graphical interface.</li>
				</ul>
				<u>General view of this API:</u>
                <br />
                <ul>
					<li>The <strong>java.lang</strong> package is a subset of the standards classes of the J2SE java.lang package.</li>
					<li>The <strong>java.io</strong> package contains the useful methods to recover data from remote systems that is to say the managing of the input/output stream.</li>
					<li>The <strong>java.util</strong> package contains a little subset of the package corresponding of J2SE. These are some classes hold: <strong>Calendar</strong>, <strong>Date</strong>, <strong>TimeZone</strong>, <strong>Enumeration</strong>, <strong>Vector</strong>, <strong>Stack</strong>, <strong>Hashtable</strong> and <strong>Random</strong>.</li>
					<li>The main objetc of the package <strong>javax.microedition.io</strong> is the class <strong>Connector</strong> letting do the inputs/outputs.</li>
				</ul>
			</div>
		</div>

		<div class="chapter">
			<h2>4. Profiles</h2>
			<p>
				A configuration defined the basement of an application, a <strong>profile</strong> offer a structure that is to say a <strong>specification giving details of the API</strong>. Profile uses a configuration.<br />
				The profile is required for furnishing a complete environment of use for one kind of equipment. It gives mainly the user interface, the input/output methods and the persistence mechanism for an equipment group. It must suffice to itself and don't need to add other java classes.<br />
				However some profiles can be created in order to reply to some equipment's capabilities or services (RMI, multimedia...).<br />
				So, equipment can support several profiles. The profile implementation is under the responsibility of the equipment's maker who imports the JVM on his equipment.<br />
				<br />
				<em>There are several profiles:</em><br />
			</p>
			<table border="1">
				<tbody>
					<tr>
						<td><strong><u>PROFILE</u></strong></td>
						<td><strong><u>CONFIGURATION</u></strong></td>
						<td><strong><u>JSR</u></strong></td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>MIDP 1.0</td>
						<td>CLDC</td>
						<td>37</td>
						<td>Furnish a collection of APIs (network management included, local stockage and the user interface)</td>
					</tr>
					<tr>
						<td>Foundation Profile</td>
						<td>CDC</td>
						<td>46</td>
						<td>Profile low in the CDC stack. API of network management for the device which doesn't need graphical possibilities</td>
					</tr>
					<tr>
						<td>Personal Profile</td>
						<td>CDC</td>
						<td>62</td>
						<td>Planned to be (with the profiles under itself in the CDC stack) the substitution of J2ME of the Sun's environment PersonalJava and to furnish applications compatibility</td>
					</tr>
					<tr>
						<td>MIPD 2.0</td>
						<td>CLDC</td>
						<td>118</td>
						<td>API for the game, the noise, the security, and a record of the MIDP stack move</td>
					</tr>
					<tr>
						<td>Personal Basis Profile</td>
						<td>CDC</td>
						<td>129</td>
						<td>Build on the basic profile, this profile offer the basic user interface support and work on the base for the personal profile</td>
					</tr>
					<tr>
						<td>RMI optional Profile (Remote Method Invocation)</td>
						<td>CDC</td>
						<td>66</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>Mobile Media API (MMAPI)</td>
						<td>CLDC</td>
						<td>135</td>
						<td>An easy and extendable API in order to commend the noise and multimedia resources on a CLCD/MIDP device</td>
					</tr>
					<tr>
						<td>JDBC optional Profile</td>
						<td>CDC</td>
						<td>169</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>Wireless Messaging API (WMA) 1.1</td>
						<td>CLDC</td>
						<td>120</td>
						<td>An API to reach the wireless communication resources, as SMS which is working with any type of J2ME profile</td>
					</tr>
				</tbody>
			</table>
			<br />
			<p>
				Sun propose two references J2ME <strong>profiles</strong>: the Foundation Profile and the Mobile Information Device Profile (MIDP).
			</p>
			<div class="subChapter">
				<h3>4.1. The&nbsp;Foundation profile</h3>
				<p>
					The Foundation Profile is destined to the CDC configuration. The developers who use this ptrofile will access to a complete implementation of the J2SE functionality.
				</p>
			</div>
			<div class="subChapter">
				<h3>4.2. The&nbsp;Mobile Information Device Profile (MIDP)</h3>
				<p>
					The <strong>MIDP</strong> profile is destined to the CLDC configuration. It takes in charge a limited number of J2SE classes and defines input/output classes and specialized interfaces for a CLDC configuration.<br />
					There are currently two major implementations for MIDP profiles. One is destined to the Assitants like Palm Pilot (PalmOs) and the other is totally generic, proposed by Sun like the reference&rsquo;s implementation.<br />
					<u>The API bound to MIDP:</u>
	            </p>
				<table border="1">
					<tbody>
						<tr>
							<td colspan="2">List of the CLDC packages</td>
						</tr>
						<tr>
							<td><strong>javax.microedition.lcdui</strong></td>
							<td>provide the user interface management (controls, ...)</td>
						</tr>
						<tr>
							<td><strong>javax.microedition.midlet</strong></td>
							<td>Technical base destined to manage the midlets' life cycle</td>
						</tr>
						<tr>
							<td><strong>javax.microedition.rms</strong></td>
							<td>Light persistent database</td>
						</tr>
					</tbody>
				</table>
				<p>
					<u>J2ME architecture:</u><br />
					<img src="images/j2me_html_m612bd0c.jpg" alt="" width="100%" /><br />
					That multiplicity of profiles can generate a definite number of problems when executing an application on several peripheral because there isn't the certitude to have readily the required profiles.<br />
					In order to dodge the problem, a particular specification has been developed. This specification come from the JSR 185 works and named Java Technology for the Wireless Industry (JTWI). Indeed it impose to the peripherals, which respect it, to carry out at least: CLDC 1.0; MIDP 2.0, Wireless Messaging API 1.1 (in order to send and received SMS) and Mobile Media API 1.1 (ensure the multimedia functions). Guarantying in this way the best compatibility between the applications and the several cell phones on which they are running.<br />
					To know which machines bear which profiles you can ask about information to the makers or you can also consult the list on the Sun website: <a href="http://developers.sun.com/mobility/device/device?api=none" target="_blank">List of the equipments supporting J2ME</a>.
                </p>
			</div>
		</div>

		<div class="chapter">
			<h2>5. The Midlets</h2>
			<p>
				The applications built with MIDP are called midlets: They are classes which inherit from the abstract class javax.microedition.midlet.Midlet. This class allows the dialogue between the system and the application.
			</p>
			<div class="subChapter">
				<h3>5.1. The Midlets</h3>
				<p>
					It has got three methods permitting to manage the application's life cycle in function of the three possible states (active, paused, destroyed):
	            </p>
				<ul>
					<li>startApp(): this method is called at each startup or reset of the application</li>
					<li>pauseApp(): this method is called when the application is paused</li>
					<li>destroyApp(): this method is called when the application is destroyed</li>
				</ul>
				<br />
				The midlet life cycle is alike to the applet's one. It has some states:<br />
				<ul>
					<li>paused:</li>
					<li>active:</li>
					<li>destroyed:</li>
				</ul>
				<br />
				The midlet state change can be caused by the executing environment or by the Midlet.<br />
				<img src="images/j2me_html_1a79b4e9.png" alt="" width="100%" />
	        </div>
			<div class="subChapter">
				<h3>5.2. A Midlet example : Francs Euros Converter</h3>
				<p>
					Sun proposed a tool in order to develop J2ME application which uses CLDC/MIDP. This tool can be download <a href="http://java.sun.com/products/sjwtoolkit/index.html" target="_blank">right here</a>. <a href="http://java.sun.com/products/sjwtoolkit/download-2_2.html" target="_bank">Download</a> it and install it. It's good to know that the Sun's <a href="http://developers.sun.com/downloads/" target="_blank">JDK standard</a> must be installed before.<br />
					Launch the KToolbar program, the following window will appear.<br />
					<img src="images/j2me_html_m27de4391.png" width="100%" /><br />
					Click "<em>New Project</em>", and write in the two fields "<em>Convertisseur</em>" which will be the project name.<br />
					On the next window, "<em>Settings for project Convertisseur</em>", pick "<em>CLDC 1.1</em>" then click "<em>OK</em>". A new folder has been created and the console displays the path.<br />
					Go on the projet folder, it contains some other folders. One of them is "<em>src</em>". In this folder create a file named "<em>Convertisseur.java</em>", open in with a text editor (like notepad) and then past the following code:<br />
					<textarea name="code" class="java">
/*
 * Convertisseur.java 1.2 31/05/06
 *
 */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public final class Convertisseur extends MIDlet implements CommandListener {
	/** Nomber of possible figure in the text field **/
	private static final int NUM_SIZE = 20;
	/** Exit Button **/
	private final Command exitCmd = new Command("Exit", Command.EXIT, 2);
	/** Calcul Button **/
	private final Command calcCmd = new Command("Calc", Command.SCREEN,1);
	private final ChoiceGroup euroFrcs = new ChoiceGroup ("", ChoiceGroup.POPUP, new String[] {"En Francs:", "En Euros:"}, null);
	/** Editable field **/
	private final TextField aConvertir = new TextField(null, "", NUM_SIZE, TextField.DECIMAL);
	private final TextField Result = new TextField(null, "", NUM_SIZE, TextField.UNEDITABLE);
	private final Alert alert = new Alert ("Error", "", null, AlertType.ERROR);
	private final double euro = 6.55957;
	/** Show if the application is running **/
	private boolean isInitialized = false;

	protected void startApp() {
		if (isInitialized)
			return;
		/* Create an object form on which we can put the components */
		Form myForm = new Form("Convertisseur Frcs-Euro");
		/* Add the components into the form */
		myForm.append("Somme &agrave; convertir :");
		myForm.append(aConvertir);
		myForm.append(euroFrcs);
		myForm.append("R&eacute;sultat = ");
		myForm.append(Result);
		myForm.addCommand(exitCmd);
		myForm.addCommand(calcCmd);
		myForm.setCommandListener(this);
		Display.getDisplay(this).setCurrent(myForm);
		alert.addCommand(new Command("Back", Command.SCREEN, 1));
		isInitialized = true;
	}

	protected void destroyApp(boolean unconditional) {}

	protected void pauseApp() {}

	public void commandAction(Command c, Displayable d) {
		/* When clicking the Exit button */
		if (c == exitCmd) {
			/* manual callto the close function */
			destroyApp(false);
			notifyDestroyed();
			return;
		}
		/* Nota: the "float" type is only known since the CLDC 1.1 */
		double res = 0.0;
		try {
			double n1 = getNumber(aConvertir, "First");
			/** Euro-&gt;Francs or Francs-&gt;Euro **/
			switch (euroFrcs.getSelectedIndex()) {
				case 0: res = n1 * euro; break;
				case 1: res = n1 / euro; break;
				default:
			}
		} catch(NumberFormatException nfe) {
			return;
		}
		String res_str = Double.toString(res);
		/* Resizing the Result window in fonction of the result */
		if (res_str.length() &gt; Result.getMaxSize())
			Result.setMaxSize(res_str.length());
		/* Displays the result */
		Result.setString(res_str);
	}

	/** Method testing the number to convert **/
	private double getNumber(TextField t, String type) throws NumberFormatException {
		String s = t.getString();
		/* testing if the field Somme to convert is full */
		if (s.length() == 0) {
			alert.setString("No " + type + " Argument");
			Display.getDisplay(this).setCurrent(alert);
			throw new NumberFormatException();
		}
		double n;
		try {
			n = Double.parseDouble(s);
			/* testing if the number is negativ or not */
			if (n &lt; 0) {
				Display.getDisplay(this).setCurrent(alert);
				throw new NumberFormatException();
			}
		} catch (NumberFormatException nfe) {
			alert.setString(type + " argument is out of range.");
			Display.getDisplay(this).setCurrent(alert);
			throw nfe;
		}
		return n;
	}
}
					</textarea>
					<br />
					<u>Let's analyze this code:</u><br />
					First of all we start with the import of the 2 packages javax.microedition.midlet and javax.microedition.lcdui. Those packages offer respectively classes for the midlets' life management and for the GUI (Graphical User Interface).<br />
					<br />
					The first part defines controls and the graphical components: The Command class defines the buttons into our application. A command (<strong>Command</strong>) encapsulates the name (which is displayed), the type (intent of the command like BACK, CANCEL, and OK) and the action priority. The ChoiceGroup class defines a selection of one ore more elements. The TextField class defines an input text component. These two classes inherit the javax.microedition.lcdui.Item class.<br />
					<br />
					The Alert class can display a dialog box during a determined time. It has got two constructors:<br />
				</p>
				<ul>
					<li>one requiring the object title</li>
					<li>one requiring the title, the text, the picture and the picture type</li>
				</ul>
				<br />
				AlertType.ERROR can inform the user if there is any error.<br />
				<br />
				The startApp() method is called at each startup or reset of the midlet.<br />
				The pauseApp() method is called when the midlet is paused.<br />
				The destroyApp() method is called when the midlet is destroyed.<br />
				<br />
				An active midlet has normally access to the screen. This one is depicted by the Display class and the unique instance you can have is recovered while calling Display.getDisplay(m) where m is the midlet. The midlet can change the object to be displayed with the Display.setCurrent call.<br />
				<br />
				The commandAction() method in the callback (listener) object take in argument the command <strong>Command</strong> and the activated object <strong>Displayable</strong>.<br />
				<br />
				In order to build the project, you juste have to click "<em>Build</em>"<br />
				<img src="images%5Cj2me_html_m5fbbd4e6.png" alt="" /><br />
				<br />
				To execute this project you have to chose the type of the emulator to use and then click "<em>Run</em>".<br />
				Two important files (Convertisseur.jar and Convertisseur.jad) are generated in the "<em>bin</em>" folder.<br />
				<br />
				<u>Example</u>: with the default cell phone emulator.<br />
				<br />
				<img src="images/j2me_html_m7c59158.png" alt="" />
				<br />
	            <img src="images/j2me_html_3f3c1c0f.png" alt="" />
	        </div>
		</div>

		<div class="chapter">
			<h2>6. Conclusion</h2>
			<p>
				The J2ME platform is a powerful tool for the cell phone programming. There are no doubts this technology will become better and better through the next years.<br />
				Moreover Java isn't alone on this increasing market, indeed Microsoft is also in it.<br />
				Sun has got strong partners such as IBM or Oracle on this embed market. The allies don't lack either with the cell phone makers (Nokia, Motorola, Siemens, Sony). Microsoft needs to be very dynamic in order to win against Java on this highly strategic market promised to a successful future.
			</p>
		</div>

	</body>
</html>
